package com.duowei.diandan.sql;

import android.os.Handler;
import android.support.annotation.WorkerThread;
import android.util.Log;

import com.duowei.diandan.app.OrderList;
import com.duowei.diandan.bean.Pbdyxxb;
import com.duowei.diandan.constant.NetConfig;
import com.duowei.diandan.constant.Settings;
import com.duowei.diandan.db.AppDatabase;
import com.duowei.diandan.db.entry.Wmlsb;
import com.duowei.diandan.db.entry.Wmlsbjb;
import com.duowei.diandan.util.NetUtils;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

/**
 * 中西餐数据提交
 */
public class OrderCommit {
    private static final String TAG = "OrderCommit";

    private Handler mHandler = new Handler();
    private AppDatabase mDb = AppDatabase.getDatabase();

    private Wmlsbjb mWmlsbjb;
    private List<Wmlsb> mWmlsbList;

    public OrderCommit() {
    }

    public void createWmlsbjbOnSqlServer(OnStatusListener listener) {
        new Thread(() -> {
            Wmlsbjb wmlsbjb = mDb.wmlsbjbDao().findOne();

            try {
                NetUtils.post7(NetConfig.getServerUrl(), SqlHelper.toInsertSql(wmlsbjb));

                mHandler.post(() -> {
                    if (listener != null) {
                        listener.onSuccess();
                    }
                });
            } catch (IOException e) {
                Log.e(TAG, "createWmlsbjbOnSqlServer: ", e);

                mHandler.post(() -> {
                    if (listener != null) {
                        listener.onFail();
                    }
                });
            }
        }).start();
    }

    public void orderCommitToSqlServer(OnStatusListener listener) {
        new Thread(() -> {
            handleWmlsbjbAndWmlsb();

            StringBuilder sql = new StringBuilder();
            mWmlsbjb.setBy12("2");
            sql.append(SqlHelper.toUpdateSql(mWmlsbjb, "wmdbh = '" + mWmlsbjb.getWmdbh() + "'"));

            for (Wmlsb w : mWmlsbList) {
                w.setSfyxd("0");
                sql.append(SqlHelper.toInsertSql(w));
            }

            // 平板打印
            String insertPbdyxxb = Pbdyxxb.toInsertString2(
                    mWmlsbjb.getWmdbh(),
                    mWmlsbjb.getZh(),
                    Settings.DEVICE_NAME,
                    mWmlsbjb.getJcrs() != null ? String.valueOf(mWmlsbjb.getJcrs()) : "1"
            );
            sql.append(insertPbdyxxb);
            sql.append(String.format(Locale.CHINA, "UPDATE wmlsb SET sfyxd = '1' WHERE wmdbh " +
                    "= '%s' AND sfyxd <> 1|", mWmlsbjb.getWmdbh()));

            try {
                NetUtils.post7(NetConfig.getServerUrl(), sql.toString());

                mDb.wmlsbDao().deleteAllLocal();

                mHandler.post(() -> {
                    if (listener != null) {
                        listener.onSuccess();
                    }
                });
            } catch (IOException e) {
                Log.e(TAG, "orderCommitToSqlServer: ", e);

                mHandler.post(() -> {
                    if (listener != null) {
                        listener.onFail();
                    }
                });
            }

        }).start();
    }


    @WorkerThread
    private void handleWmlsbjbAndWmlsb() {
        mWmlsbjb = mDb.wmlsbjbDao().findOne();
        mWmlsbList = mDb.wmlsbDao().findAllLocal();

        for (Wmlsb w : mWmlsbList) {
            w.setWmdbh(mWmlsbjb.getWmdbh());
            w.setSyyxm(Settings.DEFAULT_YHJBQK.getYhmc());

            mDb.wmlsbDao().update(w);
        }
        mWmlsbjb.setYs(OrderList.getInstance().getMoney().getTotalFloat());

        mDb.wmlsbjbDao().update(mWmlsbjb);
    }

    public interface OnStatusListener {
        void onSuccess();

        void onFail();
    }
}
